<?php

class admin_users_controller{
    private $error_message;

    public function exec_index(){
        //This function return the list of users
        global $dbdefault;
        $page = GET_value('p',1);
        $query = "SELECT * FROM users";
        $result = mysql_query($query,$dbdefault) or die(__CLASS__."->".__METHOD__.": ".mysql_err());
        $users=array();
        while($r = mysql_fetch_assoc($result)){
            $users[]=$r;
        }
        $tpl = new Template(MODULE_ROOT.'/admin_users/view/index.html');
        $tpl ->set('users', $users);
        $tpl ->set('page', $page);
        $content = $tpl ->fetch();
        return array('content'=>$content);
    }
    
    public function exec_create(){
        //This function create a new users
        $status = GET_value('s');
        $data['user_name'] = strtolower (POST_value('user_name',''));
        $data['user_password'] = POST_value('user_password','');
        $data['user_password1'] = $data['user_password'];
        $data['user_email'] = strtolower (POST_value('user_email',''));
        $data['user_realname'] = POST_value('user_realname','');
        $data['group_id'] = POST_value('group_id',0);
        if ('submit' == $status){
            //Do insert stuff
            $r = User :: create($data);
            if ($r['success']){
                $content='<div class="success" style="margin:10px">User '.$data['user_name'].' is successfully created</div>';
                $index = administration_controller::exec_index();
                $content.=$index['content'];
            }else{
                $content = '';
                if (!empty($r['error'])){ 
                    $content = '<div class="error" style="margin:10px">'.$r['error'].'</div>';
                }
                $content .= $this->show_create_form($data, $r['form_error']);
            }
        }else{
            //Just show form
            $content = $this->show_create_form($data, null);
        }
        
        return array('content'=>$content);
    }

    public function exec_edit(){
        //This function edit an existed users
        $status = GET_value('s');
        $user_id = (int) GET_value('user_id',0);
        
        if ('submit' == $status){
            $data['user_id'] = $user_id; 
            $data['user_name'] = POST_value('user_name','');
            $data['user_password'] = POST_value('user_password','');
            $data['user_password1'] = $data['user_password'];
            $data['user_email'] = strtolower (POST_value('user_email',''));
            $data['user_realname'] = POST_value('user_realname','');
            $data['group_id'] = POST_value('group_id',0);
            $r = User :: update($user_id,$data);
            if ($r['success']){
                $content = '<div class="success">Update successful!</div>';
                $index = $this->exec_index();
                $content.=$index['content']; 
            }else{
                $content='';
                if (isset($r['error'])) $content .= '<div class="error">Something wrong! '.$r['error'].'</div>';
                $content .= $this->show_edit_form($data, $r['form_error']);
            }
        }else{
            $r = User :: get_user_data($user_id);
            if ($r['success']){
                $content = $this->show_edit_form($r['data'], null);
            }else{
                $content = $this->exec_error($r['error']);
            }
        }
        return array('content' => $content);
    }
    
    public function exec_delete(){
        //This function delete an existed users
        //$status = GET_value('s');
        $user_id = (int) GET_value('user_id',0);

        $r = User :: delete($user_id);
        if ($r['success']){
            $content = '<div class="success">Delete user successful!</div>';
            $index = $this->exec_index();
            $content.=$index['content']; 
        }else{
            $content='';
            if (isset($r['error'])) $content .= '<div class="error">Something wrong! '.$r['error'].'</div>';
            $index = $this->exec_index();
            $content .= $index['content'];
        }
        return array('content' => $content);
    }

    public function exec_error(){
        return array('content'=>'<div class="error">Admin users: '.$this->error_message.'</div>');
    }
    
    //
    private function show_create_form($data,$error){
        $tpl = new Template(MODULE_ROOT.'/admin_users/view/create_form.html');
        $tpl ->set('data', $data);
        $tpl ->set('error', $error);
        return $tpl ->fetch();
    }
    //
    private function show_edit_form($data,$error){
        $tpl = new Template(MODULE_ROOT.'/admin_users/view/edit_form.html');
        $tpl ->set('data', $data);
        $tpl ->set('error', $error);
        return $tpl ->fetch();
    }
}